Variable Declaration


In JavaScript, variables can be declared using var, let, or const. Each keyword has its own scope and usage.

1. Using var

The var keyword declares a variable that is function-scoped or globally-scoped.

var x = 5;
console.log(x); // Outputs: 5

2. Using let

The let keyword declares a block-scoped variable, which means it is only accessible within the block it is declared.

let y = 10;
console.log(y); // Outputs: 10

3. Using const

The const keyword declares a block-scoped variable that cannot be reassigned after its initial declaration.

const z = 15;
console.log(z); // Outputs: 15